package com.eascs.workflow.sys.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.workflow.common.dao.BaseDao;
import com.eascs.workflow.common.entity.sys.SysRole;
import com.eascs.workflow.common.entity.sys.SysRoleResources;
import com.eascs.workflow.common.entity.sys.SysUser;
import com.eascs.workflow.sys.dao.SysRoleDao;

@Repository
public class SysRoleDaoImpl extends BaseDao implements SysRoleDao{
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
    /**
     * 根据用户 查询角色
     * @param
     * @return
     */
    public List<SysRole> selectRolesByUser(SysUser user) {
    	if(null == user.getEmployeeNo() || user.getEmployeeNo().equals("null")){
			logger.info("error,method:SysUserDaoImpl.selectRolesByUser必要参数不能为空!");
			return null;
		}
    	StringBuffer sql=new StringBuffer();
        sql.append(" SELECT role.id,role.role_name,role.role_level,role.description,role.creator,role.updator,role.create_time,role.update_time");
        sql.append(" FROM  act_org_role_user sur ");
        sql.append(" LEFT JOIN o2o_act_role role ON role.id = sur.role_id");
        sql.append(" WHERE sur.user_no = ? and sur.is_remove = 0 ");
        List<SysRole> list = workflowJdbcSession.findEntitys(sql.toString(), SysRole.class, new Object[] { user.getEmployeeNo() });
        return list;
    }
    
    @Override
    public List<SysRole> selectRolesByUserNo(String userNo) {
    	StringBuffer sql=new StringBuffer();
        sql.append(" SELECT role.id,role.role_name,role.role_level,role.description,role.creator,role.updator,role.create_time,role.update_time");
        sql.append(" FROM  act_org_role_user sur ");
        sql.append(" LEFT JOIN o2o_act_role role ON role.id = sur.role_id");
        sql.append(" WHERE sur.user_no = ?");
        List<SysRole> list = workflowJdbcSession.findEntitys(sql.toString(), SysRole.class, userNo);
        return list;
    }

	@Override
	public PageResult<Object[]> selectRolesPage(SysRole param,int pageSize,int pageIndex) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, pageSize, pageIndex);
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT role.id,role.role_code,role.role_name,role.description,role.creator,role.updator,role.create_time,role.update_time FROM  o2o_act_role role");
		sql.append(" WHERE 1=1");
		if (StringUtil.hasText(param.getRoleName())) {
			sql.append(" and role.role_name like ?");
			sqlPageQuery.addLikeParam(param.getRoleName());
		}
		if (StringUtil.hasText(param.getRoleCode())) {
			sql.append(" and role.role_code like ?");
			sqlPageQuery.addLikeParam(param.getRoleCode());
		}
		if (StringUtil.hasText(param.getRoleCategory())) {
			sql.append(" and role.role_category = ?");
			sqlPageQuery.addLikeParam(param.getRoleCategory());
		}
		sqlPageQuery.setQuerySql(sql.toString());
		return workflowJdbcSession.findDataWithPage(sqlPageQuery);
	}

	@Override
	public SysRole selectRolesByRole(SysRole sysRole) {
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT role.id,role.role_code,role.role_name,role.role_level,role.description,role.creator,role.updator,role.create_time,role.update_time,role.role_category FROM  o2o_act_role role");
		sql.append(" WHERE 1=1");
		List<Object> paramsList=new ArrayList<>();
		if (StringUtil.hasText(sysRole.getId())) {
			sql.append(" and role.id = ?");
			paramsList.add(sysRole.getId());
		}
		if (StringUtil.hasText(sysRole.getRoleName())) {
			sql.append(" and role.role_name = ?");
			paramsList.add(sysRole.getRoleName());
		}
		if (StringUtil.hasText(sysRole.getRoleCode())) {
			sql.append(" and role.role_code = ?");
			paramsList.add(sysRole.getRoleCode());
		}
		Object[] params =paramsList.toArray();
		return workflowJdbcSession.findEntity(sql.toString(), SysRole.class, params);
	}

	@Override
	public Integer insertRole(SysRole sysRole) {
		return workflowJdbcSession.insert(sysRole);
	}

	@Override
	public Integer deleteRole(SysRole sysRole) {
		return workflowJdbcSession.deleteByIdWithEntity(sysRole);
	}

	@Override
	public Integer updateRole(SysRole sysRole) {
    	return workflowJdbcSession.updateById(sysRole);
	}

	@Override
	public Integer insertRoleResBatch(List<SysRoleResources> roleResList) {
		int count=0;
		for (SysRoleResources r : roleResList) {
			count += workflowJdbcSession.insert(r);
		}
		return count;
	}

	@Override
	public Integer deleteRoleRes(String roleId) {
		StringBuffer sql=new StringBuffer();
    	sql.append("DELETE FROM o2o_act_role_resources WHERE role_id =?");
    	return workflowJdbcSession.execute(sql.toString(), new Object[]{roleId});
	}
    
	@Override
	public List<SysRole> getRoleList() {
		String sql = "SELECT * FROM o2o_act_role";
		return super.findEntitys(sql, SysRole.class);
	}
	
	@Override
	public SysRole getById(String roleId) {
		return super.getById(SysRole.class, roleId);
	}
	
	@Override
	public SysRole getByRoleCode(String roleCode) {
		String sql = "SELECT * FROM o2o_act_role WHERE role_code = ?";
		return super.findEntity(sql, SysRole.class, roleCode);
	}
}
